/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cx1.web.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.spring.web.servlet.view.JsonView;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.lang.StringUtils;
import org.cx1.model.Listing;
import org.cx1.model.User;
import org.cx1.model.utils.ServiceResponse;
import org.cx1.model.utils.SystemProperties;
import org.cx1.service.api.IInquiryService;
import org.cx1.service.api.IListingService;
import org.cx1.service.api.IUserService;
import org.cx1.service.impl.DListingService;
import org.cx1.service.impl.DUserService;
import org.cx1.service.impl.MInquiryService;
import org.cx1.service.impl.MListingService;
import org.cx1.web.controller.action.InquiryAction;
import org.cx1.web.controller.action.ListingAction;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author ECH
 */
public class InquiryControllerWS extends BaseController {
    
    //private IListingService listingService = new MListingService();
    
    private IListingService listingService = new DListingService();
    private IInquiryService inquiryService = new MInquiryService();
    
    public InquiryControllerWS() {
        
    }
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
                                                 HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView();
        Map map = new HashMap();        
        
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        if (ServletFileUpload.isMultipartContent(request)) {
            parameterMap = this.getMultipartFormParams(request);
        } 
        else {
            parameterMap = this.getFormParams(request);
        }
        
        Map<String, String> formfields = (Map<String, String>) parameterMap.get("formfields");
        int action = StringUtils.isEmpty(formfields.get("a")) ? 0 : Integer.parseInt(formfields.get("a"));
        
        switch (action) {         
            case InquiryAction.SUBMIT_TOUR_SPACE_INQUIRY: {
                submitTourSpaceInquiry(mav, map, request);
                break;
            }
            case InquiryAction.READ_CONTACT_DETAILS: {
                readContactDetails(mav, map, request);
                break;
            }
            default: {
                break;
            }
        }          
                
        map.put("host", SystemProperties.SYSTEM_HOST);
        map.put("port", SystemProperties.SYSTEM_PORT);
        
        mav.setView(new JsonView());
        mav.addObject("model", map);
        return mav;
    }    
    
    private void submitTourSpaceInquiry(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean success = false;
        
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");    
        String message = request.getParameter("message");   
        String listingID = request.getParameter("listingID");       
        
        //String remoteAddr = request.getRemoteAddr();
        //ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        //reCaptcha.setPrivateKey("6LcJpegSAAAAAJzGZN-OWFnq48drxH-vmu0zYHkE");

        //String challenge = request.getParameter("recaptcha_challenge_field");
        //String uresponse = request.getParameter("recaptcha_response_field");
        //ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

        if (true) {
            success = true;            
            ServiceResponse<Listing> sr1 = listingService.read(listingID);
            String creatorID = "";
            
            if (sr1.isSuccessful() && sr1.getResponse() != null) {
                Listing listing = sr1.getResponse();
                creatorID = listing.getCreatorID();
            }
            ServiceResponse<String> response = inquiryService.submitTourSpaceInquiry(name, email, phone, message, listingID, creatorID);
        } 
        else {
            success = false;
        }        
        map.put("success", success);        
    }
    
    
    private void readContactDetails(ModelAndView mav, Map map, HttpServletRequest request) {
        boolean success = true;        
        String userID = request.getParameter("ownerID");  
        String requestAt = request.getParameter("at");  
        String sessionAt = (String) request.getSession().getAttribute("at");
        try { userID = new StringBuilder().append(userID).reverse().toString(); } catch (Exception ex) { }
        
        if ( !StringUtils.isEmpty(requestAt) &&
             !StringUtils.isEmpty(sessionAt) &&
             StringUtils.containsIgnoreCase(requestAt, sessionAt)) {
            
            IUserService userService = new DUserService();
            ServiceResponse<User> response1 = userService.read(userID);
            User user = response1.getResponse();
            
            if (user != null) {
                String firstName = user.getFirstName();
                String lastName = user.getLastName();
                String email = user.getEmail();
                String mobile = user.getMobilePrimary();
                String landline = user.getLandlinePrimary();

                map.put("firstName", firstName);
                map.put("lastName", lastName);
                map.put("email", email);
                map.put("mobile", mobile);
                map.put("landline", landline);                
            }
        }
        map.put("success", success);  
    }
    
}